#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H

#include "qgl.h"
#include "Camera.h"
#include "PointCloud.h"

class OpenGLWidget : public QGLWidget
{
    Q_OBJECT

public:

    OpenGLWidget(QWidget * parent = 0);
    virtual ~OpenGLWidget();
    inline PointCloud* getPointCloud(){return p_point_cloud; }
    inline void setPointCloud(PointCloud * point_cloud){p_point_cloud= point_cloud;}
    inline QList<Camera> * getCameraList(){return p_camera_list;}
    inline void setCameraList( QList<Camera> * camera_list){p_camera_list = camera_list;}

    void updateOrigin();

    inline void setCameraSelection(int id) { m_selected_image_camera = id; }

    inline void resetRotation() { m_rotation = Vector3D(0, 0, 0); }

private:

    Vector3D m_origin;
    Vector3D m_rotation;
    float m_distance;
    float m_scale;

    PointCloud * p_point_cloud;

    QList<Camera> * p_camera_list;

    int m_selected_image_camera;

    Camera m_image_camera;
    Camera m_current_camera;

    int m_screen_width;
    int m_screen_height;

    // current pespective matrix for picking
    GLdouble m_perspective_matrix[16];

    // current modelview matrix for picking
    GLdouble m_modelview_matrix[16];

protected:

    void updateProjection();

    virtual void initializeGL();
    virtual void paintEvent(QPaintEvent * event);
    virtual void resizeGL(int width, int height);

    void pushAttribute();
    void popAttribute();
    void beginRendering();
    void processRendering();
    void endRendering();
    void beginOverpainting();
    void processOverpainting();
    void endOverpainting();

protected slots:
    void refresh();

protected:

    virtual void mousePressEvent(QMouseEvent * event);
    virtual void mouseReleaseEvent(QMouseEvent * event);
    virtual void mouseDoubleClickEvent(QMouseEvent * event);
    virtual void mouseMoveEvent(QMouseEvent * event);
    virtual void enterEvent(QEvent * event);
    virtual void leaveEvent(QEvent * event);
    virtual void wheelEvent(QWheelEvent * event);
    virtual void keyPressEvent(QKeyEvent * event);
    virtual void keyReleaseEvent(QKeyEvent * event);
};

#endif // OPENGLWIDGET_H
